package com.dqhl.business.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.business.Base.BaseFragment;
import com.dqhl.business.R;
import com.dqhl.business.adapter.OrderAdapter;
import com.dqhl.business.modle.Order;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.Dlog;
import com.dqhl.business.util.JsonUtils;
import com.dqhl.business.view.OrderSpinnerPopupWindow;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * description: 订单Fragment
 * autour: guanluocang
 * date: 2016/11/3 15:46
 */
public class OrderFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener,
        OrderAdapter.OnDeliverListener {

    private LinearLayout ll_upLoad;
    private LinearLayout ll_goods;
    private LinearLayout ll_sign;
    private OrderSpinnerPopupWindow mOrderSpinnerPopupWindow;
    private TextView tv_orderStatus;
    private ImageView iv_unfinish;
    private ImageView iv_finish;
    private ImageView iv_goods;
    private ImageView iv_ungoods;
    private Context context;
    private ListView lv_order;
    private TextView tv_no_data;
    private List<Order> orderList;
    private OrderAdapter orderAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);
        context = getActivity();
        initView(view);
        initData("1");
        return view;
    }

    private void initView(View view) {
        (ll_upLoad) = (LinearLayout) view.findViewById(R.id.ll_upLoad);
        ll_upLoad.setOnClickListener(this);
        (tv_orderStatus) = (TextView) view.findViewById(R.id.tv_orderStatus);
        (iv_finish) = (ImageView) view.findViewById(R.id.iv_finish);
        (iv_unfinish) = (ImageView) view.findViewById(R.id.iv_unfinish);
        (ll_goods) = (LinearLayout) view.findViewById(R.id.ll_goods);
        (iv_ungoods) = (ImageView) view.findViewById(R.id.iv_ungoods);
        (iv_goods) = (ImageView) view.findViewById(R.id.iv_goods);
        iv_goods.setOnClickListener(this);
        iv_ungoods.setOnClickListener(this);
        iv_finish.setOnClickListener(this);
        iv_unfinish.setOnClickListener(this);
        (lv_order) = (ListView) view.findViewById(R.id.lv_order);
        (ll_sign) = (LinearLayout) view.findViewById(R.id.ll_sign);
        lv_order.setOnItemClickListener(this);
        tv_no_data = (TextView) view.findViewById(R.id.tv_no_data);
    }

    /**
     * 订单列表
     *
     * @param state 订单状态 1.代发货 2.代签收 3.待确认收货 4.已确认收货
     */
    private void initData(String state) {
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.order_list);
        params.addBodyParameter("seller_id", user.getUser_id());
        params.addBodyParameter("state", state);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getData(result);
                if (0 == errCode) {
                    tv_no_data.setVisibility(View.GONE);
                    lv_order.setVisibility(View.VISIBLE);
                    orderList = JSON.parseArray(data, Order.class);
                    orderAdapter = new OrderAdapter(context, orderList, OrderFragment.this);
                    lv_order.setAdapter(orderAdapter);
                } else {
                    tv_no_data.setVisibility(View.VISIBLE);
                    lv_order.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_upLoad:
                showStatusPopWindow();
                break;
            //待签收
            case R.id.iv_finish:
                initData("2");
                iv_finish.setImageResource(R.drawable.ic_unsignpressed);
                iv_unfinish.setImageResource(R.drawable.ic_undeliver);
                break;
            //待发货
            case R.id.iv_unfinish:
                initData("1");
                iv_finish.setImageResource(R.drawable.ic_unsign);
                iv_unfinish.setImageResource(R.drawable.ic_undeliverpressed);
                break;
            // 已确认收货
            case R.id.iv_goods:
                iv_goods.setImageResource(R.drawable.ic_goodspressed);
                iv_ungoods.setImageResource(R.drawable.ic_ungoods);
                initData("4");
                break;
            //待确认收货
            case R.id.iv_ungoods:
                iv_goods.setImageResource(R.drawable.ic_goods);
                iv_ungoods.setImageResource(R.drawable.ic_ungoodspressed);
                initData("3");
                break;
        }
    }

    //顶部popwindow
    private void showStatusPopWindow() {

        final List<String> stateList = new ArrayList<>();
        stateList.add("未完成");
        stateList.add("已完成");
        mOrderSpinnerPopupWindow = new OrderSpinnerPopupWindow(getActivity(), stateList,
                new OrderSpinnerPopupWindow.IOnItemSelectListener() {
                    @Override
                    public void onItemClick(int position) {
                        // 1 已完成  0 未完成
                        Dlog.e("position" + position);
                        if (position == 0) {
                            // ll_sign 待发货  待签收
                            // ll_goods 待确认发货 已确认发货
                            ll_sign.setVisibility(View.VISIBLE);
                            ll_goods.setVisibility(View.GONE);
                            initData("1");
                            iv_finish.setImageResource(R.drawable.ic_unsign);
                            iv_unfinish.setImageResource(R.drawable.ic_undeliverpressed);
                        }
                        if (position == 1) {
                            ll_sign.setVisibility(View.GONE);
                            ll_goods.setVisibility(View.VISIBLE);
                            initData("3");
                            iv_goods.setImageResource(R.drawable.ic_goods);
                            iv_ungoods.setImageResource(R.drawable.ic_ungoodspressed);
                        }
                        tv_orderStatus.setText(stateList.get(position));
                    }
                });
        mOrderSpinnerPopupWindow.setWidth(ll_upLoad.getWidth());
        mOrderSpinnerPopupWindow.showAsDropDown(ll_upLoad);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    //发货按钮
    @Override
    public void onDeliever(int position) {
        String orderState = orderList.get(position).getOrder_state();
        String sellerState = orderList.get(position).getSeller_state();
        if ("1".equals(orderState) && "1".equals(sellerState)) {
            doConfirmDeliver(position); //发货
        }
        if ("2".equals(orderState) && "1".equals(sellerState)) {
            doConfirmSign(position); //签收
        }
    }

    /**
     * 发货
     *
     * @param position
     */
    private void doConfirmDeliver(int position) {
        Dlog.e(orderList.get(position).getId());
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.order_deliver);
        params.addBodyParameter("order_id", orderList.get(position).getId());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String errMsg = JsonUtils.getErrMsg(result);
                if (0 == errCode) {
                    initData("1");
                }
                toast(errMsg);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    /**
     * 签收
     *
     * @param position
     */
    private void doConfirmSign(int position) {
        Dlog.e(orderList.get(position).getId());
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.order_sign);
        params.addBodyParameter("order_id", orderList.get(position).getId());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String errMsg = JsonUtils.getErrMsg(result);
                if (0 == errCode) {
                    initData("2");
                }
                toast(errMsg);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }
}
